package test.memcached;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import com.whalin.MemCached.MemCachedClient;
import com.whalin.MemCached.SockIOPool;

public class app {
	
	public static void main(String[] args) throws UnsupportedEncodingException {
		// grab an instance of our connection pool
		SockIOPool pool = SockIOPool.getInstance();

		// set the servers and the weights
		pool.setServers( new String[]{"192.168.5.33:11211", "192.168.5.33:11212"} );
		pool.setWeights( new Integer[]{5} );

		// set some basic pool settings
		// 5 initial, 5 min, and 250 max conns
		// and set the max idle time for a conn
		// to 6 hours
		pool.setInitConn( 5 );
		pool.setMinConn( 5 );
		pool.setMaxConn( 250 );
		pool.setMaxIdle( 1000 * 60 * 60 * 6 );

		// set the sleep for the maint thread
		// it will wake up every x seconds and
		// maintain the pool size
		pool.setMaintSleep( 30 );

		// set some TCP settings
		// disable nagle
		// set the read timeout to 3 secs
		// and don't set a connect timeout
		pool.setNagle( false );
		pool.setSocketTO( 3000 );
		pool.setSocketConnectTO( 0 );
		pool.initialize();
		MemCachedClient mcc = new MemCachedClient();
//		Map<String, Object> map = getKeysForMap(mcc);
//		for(Map.Entry<String, Object> entry:map.entrySet()) {
//			System.out.println(entry.getKey()+"---->"+entry.getValue());
//		}
//		mcc.delete("lc_plat_enter:6");
//		mcc.delete("lc_plat_enter:7");
		System.out.println(mcc.get("lc_menu:35"));
		mcc.delete("lc_menu:35");
		System.out.println(mcc.get("lc_menu:35"));
	}
	
	public static Map<String,Object> getKeysForMap(MemCachedClient mcc) throws UnsupportedEncodingException{
	    Map<String,Object> keylist=new HashMap<String,Object>();
	    //遍历statsItems 获取items:2:number=14
	    Map<String,Map<String,String>> statsItems=mcc.statsItems();
	    Map<String,String> statsItems_sub=null;
	    String statsItems_sub_key=null;
	        int items_number=0;
	        String server=null;
	        //根据items:2:number=14，调用statsCacheDump，获取每个item中的key
	    Map<String,Map<String,String>> statsCacheDump=null;
	    Map<String,String> statsCacheDump_sub=null;
	    String statsCacheDumpsub_key=null;
	    String statsCacheDumpsub_key_value=null;
	    
	    for (Iterator iterator=statsItems.keySet().iterator();iterator.hasNext();) {
	      server=(String) iterator.next();
	      statsItems_sub=statsItems.get(server);
	      //System.out.println(server+"==="+statsItems_sub);
	      for (Iterator iterator_item=statsItems_sub.keySet().iterator();iterator_item.hasNext();) {
	        statsItems_sub_key=(String) iterator_item.next();				
	        //System.out.println(statsItems_sub_key+":=:"+bb);
	        //items:2:number=14
	        if (statsItems_sub_key.toUpperCase().startsWith("items:".toUpperCase()) && statsItems_sub_key.toUpperCase().endsWith(":number".toUpperCase())){
	          items_number=Integer.parseInt(statsItems_sub.get(statsItems_sub_key).trim());
	          //System.out.println(statsItems_sub_key+":=:"+items_number);
	          statsCacheDump=mcc.statsCacheDump(new String[]{server},Integer.parseInt(statsItems_sub_key.split(":")[1].trim()), items_number);
	          
	          for (Iterator statsCacheDump_iterator=statsCacheDump.keySet().iterator();statsCacheDump_iterator.hasNext();) {
	            statsCacheDump_sub=statsCacheDump.get(statsCacheDump_iterator.next());
	            //System.out.println(statsCacheDump_sub);
	            for (Iterator iterator_keys=statsCacheDump_sub.keySet().iterator();iterator_keys.hasNext();) {
	              statsCacheDumpsub_key=(String) iterator_keys.next();
	              statsCacheDumpsub_key_value=statsCacheDump_sub.get(statsCacheDumpsub_key);							
	              //System.out.println(statsCacheDumpsub_key);//key是中文被编码了,是客户端在set之前编码的，服务端中文key存的是密文
	              //System.out.println(statsCacheDumpsub_key_value);
	              String key = URLDecoder.decode(statsCacheDumpsub_key, "UTF-8");
	              keylist.put(key, mcc.get(key));
	            }
	          }
	        }

	      }
	    }
	    return keylist;
	  }

}
